package com.example.hello.controller;

import com.example.hello.pojo.Book;
import com.example.hello.pojo.Res;
import com.example.hello.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    /**
     * GET /collection?userId=xxx
     * 根据用户ID返回该用户收藏的图书集合
     */
    @GetMapping
    public Res getFavoriteBooks(@RequestParam Integer userId) {
        try {
            List<Book> favoriteBooks = collectionService.getFavoriteBooks(userId);
            return Res.success(favoriteBooks);
        } catch (Exception e) {
            e.printStackTrace();
            return Res.error("查询收藏图书失败: " + e.getMessage());
        }
    }
}